package cn.com.duiba.millionaire.center.biz.service.impl;

import cn.com.duiba.millionaire.center.api.dto.CreditsConsumeLogDto;
import cn.com.duiba.millionaire.center.api.enums.CreditsConsumeStatusTypeEnum;
import cn.com.duiba.millionaire.center.biz.dao.CreditsConsumeLogDao;
import cn.com.duiba.millionaire.center.biz.entity.CreditsConsumeLogEntity;
import cn.com.duiba.millionaire.center.biz.service.CreditsConsumeLogService;
import cn.com.duiba.thirdparty.enums.CreditsConsumeTypeEnum;
import cn.com.duiba.wolf.utils.BeanUtils;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by xiaoxuda on 2018/2/12.
 */
@Service
public class CreditsConsumeLogServiceImpl implements CreditsConsumeLogService {
    @Autowired
    private CreditsConsumeLogDao creditsConsumeLogDao;

    private Trans2Dto trans2Dto = new Trans2Dto();
    private Trans2Entity trans2Entity = new Trans2Entity();

    @Override
    public long insert(CreditsConsumeLogDto dto) {
        if(dto == null){
            throw new RuntimeException("insert param cannot be null");
        }
        return creditsConsumeLogDao.insert(trans2Entity.apply(dto));
    }

    @Override
    public CreditsConsumeLogDto findById(Long id) {
        return trans2Dto.apply(creditsConsumeLogDao.findById(id));
    }

    @Override
    public boolean updateStatus2Fail(Long id, String devBizId, String error4Admin, String error4Developer, String error4Consumer) {
        return creditsConsumeLogDao.updateStatus2Fail(id, devBizId, error4Admin, error4Developer, error4Consumer);
    }

    @Override
    public boolean updateStatus2Success(Long id, String devBizId) {
        return creditsConsumeLogDao.updateStatus2Success(id, devBizId);
    }

    @Override
    public List<CreditsConsumeLogDto> findNoFailByConsumerIdAndBizTypeAndBizId(Long consumerId, CreditsConsumeTypeEnum bizType, String bizNo) {
        return Lists.transform(creditsConsumeLogDao.findNoFailByConsumerIdAndBizTypeAndBizId(consumerId, bizType, bizNo), trans2Dto);
    }

    static class Trans2Dto implements Function<CreditsConsumeLogEntity, CreditsConsumeLogDto>{
        @Override
        public CreditsConsumeLogDto apply(CreditsConsumeLogEntity entity) {
            if(entity == null){
                return null;
            }
            CreditsConsumeLogDto dto = BeanUtils.copy(entity, CreditsConsumeLogDto.class);
            if(entity.getBizType() != null){
                dto.setBizType(CreditsConsumeTypeEnum.getByCode(entity.getBizType()));
            }
            if(entity.getConStatus() != null){
                dto.setConStatus(CreditsConsumeStatusTypeEnum.getByCode(entity.getConStatus()));
            }
            return dto;
        }
    }

    static class Trans2Entity implements Function<CreditsConsumeLogDto, CreditsConsumeLogEntity>{
        @Override
        public CreditsConsumeLogEntity apply(CreditsConsumeLogDto dto) {
            if(dto == null){
                return null;
            }
            CreditsConsumeLogEntity entity = BeanUtils.copy(dto, CreditsConsumeLogEntity.class);
            if(dto.getBizType() != null){
                entity.setBizType(dto.getBizType().getCode());
            }
            if(dto.getConStatus() != null){
                entity.setConStatus(dto.getConStatus().getCode());
            }
            return entity;
        }
    }
}
